package service

import (
	"RestaurantApp/dao"
	"RestaurantApp/utils"
	"github.com/wonderivan/logger"
	"mime/multipart"
	"strconv"
	"time"
)

type FileService struct {
}

func (this *FileService) UploadAvator(userId string, file *multipart.FileHeader) {
	fd, err := file.Open()
	if err != nil {
		logger.Error(err.Error())
	}
	defer fd.Close() // 文件流打开了一定要关闭
	// 放在 用户 星期几  目录下
	objectPath := userId + "/" + strconv.Itoa(time.Now().Day()) + "/avator.jpg"
	err = utils.OSSBucket.PutObject(objectPath, fd)
	if err != nil {
		logger.Error(err.Error())
	}

	logger.Info("成功上传图片到阿里云OSS")
	avatorUrl := "https://restaurant-hy.oss-cn-beijing.aliyuncs.com/" + userId + "/" + strconv.Itoa(time.Now().Day()) + "/avator.jpg"
	memberDao := dao.MemberDao{}
	memberDao.UpdateAvatorUrlByUserId(avatorUrl, userId)

}
